package com.sc.ktapplication.module.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import com.sc.ktapplication.R
import com.sc.ktapplication.common.base.BaseActivity
import com.sc.ktapplication.common.util.ToastUtil
import kotlinx.android.synthetic.main.activity_feedback.*

class FeedbackActivity : BaseActivity() {

    private val viewModel by viewModels<FeedbackViewModel>()

    override fun getLayoutId(): Int {
        return R.layout.activity_feedback
    }

    override fun showErrorMsg() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.saveRet.observe(this, androidx.lifecycle.Observer{ ret ->
            dismissLoadingDialog()
            if (ret) {
                ToastUtil.snackbarShort(container,"感谢您的反馈！")
                finish()
            } else {
                ToastUtil.snackbarShort(container, "系统异常，请稍后再试！")
            }
        })
    }


    fun submit(view: View) {
        if (!validate()) return
        showLoadingDialog("正在提交，请稍后...", true)
        val title: String = etTitle.text.toString()
        val desc: String = etDesc.text.toString()
        val params = hashMapOf<String,Any>()
        params["title"] = title
        params["description"] = desc
        params["createBy"] = mUser!!.userId
        viewModel.saveFeedback(params)
    }

    private fun validate(): Boolean {
        val title: String = etTitle.text.toString()
        val desc: String = etDesc.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtil.snackbarShort(container, "请输入标题！")
            return false
        }
        if (TextUtils.isEmpty(desc)) {
            ToastUtil.snackbarShort(container, "请输入描述！")
            return false
        }
        return true
    }
}